#include "bluetooth_at.h"

#define BLUETOOTH_SCAN_DEVICE_DELAY_TIME 2000 // 蓝牙扫描设备延时时间

// add end string(追加的末尾字符)
char atReturnString[BLUETOOTH_AT_MAX_LEN];

extern uint8_t boolATFlag;

// at and state pins definition initliation(模式引脚和状态引脚初始化定义)
GPIO_InitTypeDef Bluetooth_Mode_GPIO_InitStruct;
GPIO_InitTypeDef Bluetooth_State_GPIO_InitStruct;

extern USARTBuffer bluetoothInfo;
extern USARTBuffer usart_buffer;

// Bluetooth At Init(蓝牙AT初始化)
void bluetooth_at_init(void)
{
    // device init
    // at pin init
    Bluetooth_Mode_GPIO_InitStruct.GPIO_Pin = BLUETOOTH_AT_MODE_PIN; // 引脚A1
    Bluetooth_Mode_GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;     // 输出模式
    Bluetooth_Mode_GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;    // 响应速率
    // state pin init
    Bluetooth_State_GPIO_InitStruct.GPIO_Pin = BLUETOOTH_STATE_PIN;    // 引脚A4
    Bluetooth_State_GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 输入模式
    Bluetooth_State_GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;     // 响应速度
    // at and state pin init
    GPIO_Init(GPIOA, &Bluetooth_Mode_GPIO_InitStruct);
    GPIO_Init(GPIOA, &Bluetooth_State_GPIO_InitStruct);
    // open clk
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_SetBits(GPIOA, BLUETOOTH_AT_MODE_PIN);
}

// Bluetooth At Send Command(蓝牙AT发送命令)
void bluetooth_at_send_cmd(char *String)
{
    // Read GPIO Voltage (读取GPIO电压)
    uint8_t GPIO_State_Voltage = GPIO_ReadOutputDataBit(GPIOA, BLUETOOTH_STATE_PIN); // A4, state pin
    if (GPIO_State_Voltage == 1)
    {
        // common connection
        USART_SendString(USART1, String); // 给蓝牙串口发送字符串
    }
    else
    {
        // Send 'wrong connection' to USART2 (发送'连接错误'到上位机USART2)
        USART_SendString(USART2, "Not in AT mode!\r\n");
    }
}

// Bluetooth Send Data Init(蓝牙发送数据初始化)
void bluetooth_send_data_init(void)
{
}

// Bluetooth Scan Device(蓝牙扫描设备)
void bluetooth_scan_device(void)
{
    GPIO_SetBits(GPIOA, BLUETOOTH_AT_MODE_PIN);
    uint8_t GPIO_Mode_Voltage = GPIO_ReadOutputDataBit(GPIOA, BLUETOOTH_AT_MODE_PIN); // A1, mode pin
    if (GPIO_Mode_Voltage == 0)
    {
        USART_SendString(USART2, "AT Voltage is low.\r\n"); // send info to computer usart
    }
    else
    {
        GPIO_SetBits(GPIOA, BLUETOOTH_AT_MODE_PIN);
        USART_SendString(USART2, "AT Voltage is high.\r\n");         // send info to computer usart
        get_bluetooth_return_string(AT_CMD_MODE, AT_CMD_OK);         // enter at mode
        get_bluetooth_return_string(AT_CMD_INIT_SPP, AT_CMD_OK);     // init spp
        get_bluetooth_return_string(AT_CMD_IAC, AT_CMD_OK);          // scan every reading code bluetooth device
        get_bluetooth_return_string(AT_CMD_DEVICE_CLASS, AT_CMD_OK); // scan every type of devices
        get_bluetooth_return_string(AT_CMD_INQM, AT_CMD_OK);         // scan mode
        get_bluetooth_return_string(AT_CMD_INQ, AT_CMD_OK);          // scan timeuse at cmd to scan bluetooth device
        get_bluetooth_return_string(AT_CMD_INQC, AT_CMD_OK);         // cancle buletooth usart cmd
        USART_SendString(USART2, "endCmd\r\n");                      // cancle buletooth usart cmd
    }
    GPIO_ResetBits(GPIOA, BLUETOOTH_AT_MODE_PIN); // reset mode pin
}

// Bluetooth Connect Device(蓝牙连接设备)
void bluetooth_connect_device(char *bluetADDR, uint32_t waitTime)
{
    USART_SendString(USART1, "AT+DISC\r\n"); // disconnect bluetooth device
    USART_SendString(USART1, "AT\r\n");
    char cmd_str1[] = "AT+PAIR=";
    USART_SendString(USART1, "AT\r\n"); // send AT to check if bluetooth is not in at mode
}

// Get Bluetooth Return String(获取蓝牙返回字符串)
void get_bluetooth_return_string(char *str, char *comp)
{
    USART_SendString(USART1, str);              // send at cmd to bluetooth
    delay_ms(BLUETOOTH_SCAN_DEVICE_DELAY_TIME); // delay time
    if (bluetoothInfo.USART_RX_Complete)
    {
        USART_SendString(USART2, "Error!\r\n");
        USART_SendString(USART2, "Info:");
        USART_SendString(USART2, bluetoothInfo.USART_RX_Buffer);
        USART_SendString(USART2, ".\r\n");
        bluetoothInfo.USART_RX_Complete = 0;
        bluetoothInfo.USART_RX_Index = 0;
    }
}